package file.manager;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

import android.app.Activity;
import android.os.Environment;
import android.util.Log;

public class FileManager
{
  private static final String TAG = "FileManager";
  
  public static boolean writeToFile( File root, String directoryName, String fileName, String body, boolean overwrite )
  {
    try
    {
      if( !root.canWrite() )
      {
        Log.e( TAG, "root can't be writen" );
        return false;
      }
      
      File file;
      if( directoryName != null && directoryName.length() > 0 )
      {
        File directory = new File( root, directoryName );
        if ( !directory.exists() )
        {
          if( !directory.mkdirs() )
          {
            Log.e( TAG, "mkdirs() failed: " + directory.getAbsolutePath() );
            return false;
          }
        }
        file = new File( directory, fileName );
      }
      else
      {
        file = new File( root, fileName );
      }
      
      if( file.exists() )
      {
    	  if( !overwrite )
    	  {
    		  Log.w( TAG, "file already exists: " + file.getAbsolutePath() );
    		  return true;
    	  }
    	  
    	  file.delete();
      }
      
      //file.canWrite() returns false but the file can be written in fact.
      FileWriter writer = new FileWriter( file );
      writer.append( body );
      writer.flush();
      writer.close();
      
      Log.i( TAG, "Success: " + fileName + " has been written." );
      return true;
    }
    catch ( IOException e )
    {
      e.printStackTrace();
      Log.e( TAG, "try to save file: " + e.getMessage() );
      return false;
    }
  }

  /**
   * read content from file, line by line
   * @param root
   * @param directoryName
   * @param fileName
   * @param startLine
   * @param lines
   * @param content
   * @return true is successful
   */
  public static boolean readFromFile( File root, String directoryName, String fileName, int startLine, int lines, List<String> content )
  {
	  try
	    {
	      FileReader reader = getFileReader( root, directoryName, fileName );
	      if( reader == null )
	    	  return false;
	      
	      BufferedReader br = new BufferedReader( reader );
		    String line;

		    while ((line = br.readLine()) != null) 
		    {
		    	if( line.trim().length() == 0 )
		    		continue;
		    	content.add( line );
		    }
	      reader.close();
	      
	      Log.i( TAG, "Success: " + fileName + " has been read." );
	      return true;
	    }
	    catch ( IOException e )
	    {
	      e.printStackTrace();
	      Log.e( TAG, "read file failed: " + e.getMessage() );
	      return false;
	    }
  }
  
  public static boolean readFromFile( File root, String directoryName, String fileName, StringBuilder content )
  {
    try
    {
      FileReader reader = getFileReader( root, directoryName, fileName );
      if( reader == null )
    	  return false;
      
      while( true )
      {
	      char buffer[] = new char[1024];
	      int readLen = reader.read( buffer );
	      if( readLen <= 0 )
	    	  break;
	      content.append( buffer, 0, readLen );
      }
      reader.close();
      
      Log.i( TAG, "Success: " + fileName + " has been read." );
      return true;
    }
    catch ( IOException e )
    {
      e.printStackTrace();
      Log.e( TAG, "read file failed: " + e.getMessage() );
      return false;
    }
  }
  
  public static FileReader getFileReader( File root, String directoryName, String fileName )
  {
	  try
	    {
	      File file;
	      if( directoryName != null && directoryName.length() > 0 )
	      {
	        File directory = new File( root, directoryName );
	        if ( !directory.exists() )
	        {
		        Log.e( TAG, "directory not exists: " + directory.getAbsolutePath() );
		        return null;
	        }
	        file = new File( directory, fileName );
	      }
	      else
	      {
	        file = new File( root, fileName );
	      }
	      
	      if( !file.exists() )
	      {
			  Log.e( TAG, "file not exists: " + file.getAbsolutePath() );
			  return null;
	      }
	      
	      if( !file.canRead() )
	      {
	    	Log.e( TAG, "the file can't be read: " + file.getAbsolutePath() );
	        return null;
	      }
	      
	      return new FileReader( file );
	    }
	  catch ( IOException e )
	    {
	      e.printStackTrace();
	      Log.e( TAG, "get file reader failed: " + e.getMessage() );
	      return null;
	    }
  }

}
